layui.config({
    base: '/home/js/util/'
}).use(['element','laypage','jquery','menu','carousel'],function(){
    element = layui.element,
        laypage = layui.laypage,
        $ = layui.$,
        carousel = layui.carousel,
        menu = layui.menu;

    //常规轮播
    carousel.render({
        elem: '#home-carousel'
        ,arrow: 'always'
        ,width: '100%'
        ,height: '600px'
        ,autoplay: true
    });

    //判断是否有用户登录
    var checkLoginState = function () {

        var userInfo = window.sessionStorage.getItem("home-member");
        if (userInfo == null || userInfo == '' || userInfo == undefined){
            return false;
        }
        return true;
    };


    var rendData = function (news) {
        var newshtml = '';


        if (news == null){
            return;
        }
        $.each(news,function (index,value) {
            newshtml += '        <div class="item">\n' +
                '          <div class="layui-fluid">\n' +
                '            <div class="layui-row">\n' +
                '              <div class="layui-col-xs12 layui-col-sm4 layui-col-md5">\n' +
                '                <div class="img"><img src="'+value.newsHomeImgurl+'" alt=""></div>\n' +
                '              </div>\n' +
                '              <div class="layui-col-xs12 layui-col-sm8 layui-col-md7">\n' +
                '                <div class="item-cont">\n' +
                '                  <h3 ><a target="_parent" href="/home/details.html?news='+value.newsId +'" style="color:#404958;cursor:pointer;" >'+value.newsTitle +'</a>' +
                '                       <button class="layui-btn layui-btn-danger new-icon">热门</button></h3>\n' +
                '                  <h5>'+value.newsType +'</h5>\n' +
                /*这里有个bug 剪切新闻类容前一部分用来当大致描述，会出现剪切p标签不完整，照成后面a标签错乱无效，或者排版边乱*/
                //2020/12/27 通过数据库截取字符串函数解决bug 这里取消了字符串截取
                '                  <p>'+value.newsContent+'......</p></p>\n' +
                '                  <a target="_parent"  href="/home/details.html?news='+value.newsId +'" class="go-icon"></a>\n' +
                '                </div>\n' +
                '            </div>\n' +
                '            </div>\n' +
                '           </div>\n' +
                '        </div>\n' +
                '<hr class="layui-bg-red">\n';
        });

        $("#news_contest").html(newshtml);
    };

    var laypageTable = function (total) {
        if (total == 0){
            return;
        }
        laypage.render({
            elem: 'laypage',
            count: total,
            limit: searchParam.limit,
            curr: searchParam.page,
            layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'],
            jump: function (obj, first) {
                if (!first) {
                    searchParam.page = obj.curr;
                    searchParam.limit = obj.limit;
                    loadData();
                }
            }
        });
    };


    var searchParam = {
        req: {
            newsState : '1'
        },
        page: 1,
        limit: 10,

    };

    var loadData = function (page) {
        searchParam.page = CoreUtil.isEmpty(page) ? searchParam.page : page;
        //table数据加载
        CoreUtil.sendAjax("/news/getNewsInfo", JSON.stringify(searchParam), function (res) {
            if (res.data && res.data.records != null) {
                //layer.msg('查询成功', {icon: 1});
                rendData(res.data.records);
                laypageTable(res.data.total);
                //存储到本地缓存
                window.sessionStorage.setItem("news_list",JSON.stringify(res.data.records));
            } else {
                layer.msg('查询失败', {icon: 2});
            }
        }, "POST", false, function (res) {
            layer.msg('站点出问题啦！请联系管理员', {icon: 6})
        });
    };
    //首页，我关注的绑定点击事件。
    $("#self-view").on('click',function () {
        //移除兄弟节点 'active'
        $("#self-view").siblings().removeClass('active');
        //选中
        $("#self-view").addClass('active');
        //没有登录
        if (!checkLoginState()){
            layer.msg('您还没登录,登录后重试！', {icon: 2},function () {
                window.location.href = '/home/member-login.html';
            });
            return false;
        }

        loadSelfData();
    });
//加载我关注的数据 的分页条
    var loadSelfDatalayerpage = function(total) {
        if (total <= 1){
            return;
        }

        laypage.render({
            elem: 'laypage',
            count: total,
            limit: searchParam.limit,
            curr: searchParam.page,
            layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'],
            jump: function (obj, first) {
                if (!first) {
                    searchParam.page = obj.curr;
                    searchParam.limit = obj.limit;
                    loadSelfData();
                }
            }
        });
    };


    //加载国家大事分页条
    var importantNewsPage = function(total) {

        laypage.render({
            elem: 'laypage',
            count: total,
            limit: searchParam.limit,
            curr: searchParam.page,
            layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'],
            jump: function (obj, first) {
                if (!first) {
                    searchParam.page = obj.curr;
                    searchParam.limit = obj.limit;
                    loadImportantNewsData();
                }
            }
        });
    };

    //加载国家大事数据
    var loadImportantNewsData = function() {

        CoreUtil.sendAjax("/importantNews", JSON.stringify(searchParam), function (res) {
            if (res.data && res.data.records != null) {
                //layer.msg('查询成功', {icon: 1});
                rendData(res.data.records);
                importantNewsPage(res.data.total);
            } else {
                layer.msg('查询失败', {icon: 2});
            }
        }, "POST", false, function (res) {
            layer.msg('站点出问题啦！请联系管理员', {icon: 6})
        });
    };

    //加载我关注的数据
    var loadSelfData = function() {

        CoreUtil.sendAjax("/wisdomPush", JSON.stringify(searchParam), function (res) {
            if (res.data && res.data.records != null) {
                //layer.msg('查询成功', {icon: 1});
                rendData(res.data.records);
                loadSelfDatalayerpage(res.data.total);
            } else {
                layer.msg('查询失败', {icon: 2});
            }
        }, "POST", false, function (res) {
            layer.msg('站点出问题啦！请联系管理员', {icon: 6})
        });
    };


    //首页爆款新闻按钮
    $("#home-news").on('click',function () {
        $("#home-news").siblings().removeClass('active');
        $("#home-news").addClass('active');
        loadData();
    });

    //首页爆款新闻按钮
    $("#important-news").on('click',function () {
        $("#important-news").siblings().removeClass('active');
        $("#important-news").addClass('active');
        loadImportantNewsData();
    });

    loadData();
    menu.init();
})